<template>
  <div>
    <router-view/>
    <!-- 导航标签 -->
    <van-tabbar v-model="active">
        <van-tabbar-item to="/">
        <span>首页</span>
        <template #icon="props">
            <img :src="props.active ? require('../../assets/img/主页.png') : require('../../assets/img/主页1.png') " />
        </template>
        </van-tabbar-item>
        <van-tabbar-item to="/indent" :icon="require('../../assets/img/订单.png')">订单</van-tabbar-item>
        <van-tabbar-item to="/mine" :icon="require('../../assets/img/我的.png')">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import axios from 'axios';
export default {
    name:"HemoPage",
    data() {
        return {
            active: this.$store.state.active,
            user:{},
            // token密钥
            token:'Bearer ' + localStorage.getItem("token"),
        }
    },
    methods: {
        // 判断有没有tokn并检查是否登录
    isTokn(){
            let token = localStorage.getItem("token") ;
            
            if(token != null) {
                this.showUserInfo();
            } else {
                this.$store.state.isLogin = false
            }
        },
        showUserInfo(){
            let that = this
            console.log(this);
            const instance = axios.create({
                baseURL: 'https://api.example.com',
                timeout: 5000,
                headers: {
                    // 设置请求头，把 token 传递给服务器程序，以便于认证
                    'Authorization': that.token
                }
            });
            // 发起请求
            instance.get('http://localhost:9090/api/user')
            .then(response => {
                // 处理响应数据
                console.log(response.data);
                if (response.data.code === 200) {
                    this.$store.state.isLogin = true
                    this.user = response.data.data;
                    this.$store.state.uid = response.data.data.uid;
                } else{
                    this.$store.state.isLogin = false
                }
            })
            .catch(error => {
                // 处理错误
                console.error(error);
            });
        },
    },
    created(){
        this.isTokn();
    }

}
</script>

<style>

</style>